﻿using System.Globalization;
using System.Windows.Automation;
using ProdControls.Interfaces;
using ProdControls.UIAPatterns;
using ProdLogging;

namespace ProdControls.Bridges
{
    public static class GridItemBridge
    {
        /// <summary>
        ///   Gets the number of columns spanned by a cell or item.
        /// </summary>
        /// <param name="extension"> The extended interface. </param>
        /// <param name="control"> The UI Automation element </param>
        /// <param name="dataItem"> The data item. </param>
        /// <returns> </returns>
        public static int GetColumnSpanHook(this IGridItem extension, BaseProdControl control,
                                            AutomationElement dataItem)
        {
            return UiaGetItemColumnSpan(dataItem);
        }

        private static int UiaGetItemColumnSpan(AutomationElement dataItem)
        {
            int retVal = GridItemPatternHelper.GetColumnSpan(dataItem);
            LogController.ReceiveLogMessage(new LogMessage(retVal.ToString(CultureInfo.CurrentCulture)));
            return retVal;
        }

        /// <summary>
        ///   Gets the number of rows spanned by a cell or item.
        /// </summary>
        /// <param name="extension"> The extended interface. </param>
        /// <param name="control"> The UI Automation element </param>
        /// <param name="dataItem"> The data item. </param>
        /// <returns> </returns>
        public static int GetRowSpanHook(this IGridItem extension, BaseProdControl control,
                                         AutomationElement dataItem)
        {
            return UiaGetItemRowSpan(dataItem);
        }

        private static int UiaGetItemRowSpan(AutomationElement dataItem)
        {
            int retVal = GridItemPatternHelper.GetRowSpan(dataItem);
            LogController.ReceiveLogMessage(new LogMessage(retVal.ToString(CultureInfo.CurrentCulture)));
            return retVal;
        }

        /// <summary>
        ///   Gets the ordinal number of the column that contains the cell or item.
        /// </summary>
        /// <returns> </returns>
        public static int GetColumnHook(this IGridItem extension, BaseProdControl control,
                                        AutomationElement dataItem)
        {
            return UiaGetColumn(dataItem);
        }

        private static int UiaGetColumn(AutomationElement dataItem)
        {
            int retVal = GridItemPatternHelper.GetColumn(dataItem);
            LogController.ReceiveLogMessage(new LogMessage(retVal.ToString(CultureInfo.CurrentCulture)));
            return retVal;
        }

        /// <summary>
        ///   Gets the ordinal number of the column that contains the cell or item.
        /// </summary>
        /// <param name="extension"> The extended interface. </param>
        /// <param name="control"> The UI Automation element </param>
        /// <param name="dataItem"> The data item. </param>
        /// <returns> </returns>
        public static int GetRowHook(this IGridItem extension, BaseProdControl control,
                                     AutomationElement dataItem)
        {
            return UiaGetRow(dataItem);
        }

        private static int UiaGetRow(AutomationElement dataItem)
        {
            int retVal = GridItemPatternHelper.GetRow(dataItem);
            LogController.ReceiveLogMessage(new LogMessage(retVal.ToString(CultureInfo.CurrentCulture)));
            return retVal;
        }


        public static AutomationElement GetContainingGridHook(this IGridItem extension, BaseProdControl control,
                                                              AutomationElement dataItem)
        {
            return UiaGetContainingGrid(dataItem);
        }

        private static AutomationElement UiaGetContainingGrid(AutomationElement dataItem)
        {
            AutomationElement retVal = GridItemPatternHelper.GetContainingGrid(dataItem);
            LogController.ReceiveLogMessage(
                new LogMessage(retVal.Current.AutomationId.ToString(CultureInfo.CurrentCulture)));
            return retVal;
        }
    }
}